<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="站点管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="400px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="选择地区" prop="districtCode">
                <el-cascader
                    v-model="formData.districtCode"
                    :options="options"
                    :props="{ checkStrictly: false, emitPath:false }"
                    clearable
                    style="width:100%;"
                />
            </el-form-item>
            <el-form-item label="站点名称" prop="stationName">
                <el-input v-model="formData.stationName"/>
            </el-form-item>

            <el-form-item label="简拼" prop="stationSpell">
                <el-input v-model="formData.stationSpell"/>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                    <template #reference>
                        <el-button slot="reference" type="primary">保存</el-button>
                    </template>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {RegionApi} from '@/api/system/RegionApi'
import {StationApi} from '@/api/intercity/StationApi'
import {Common} from '@/utils/common'

export default {
    name: 'StationUpdate',
    data() {
        return {
            formData: {
                id: 0,
                districtCode: '',
                stationName: '',
                stationSpell: '',
                sort: 0
            },
            rules: {
                districtCode: [
                    {required: true, message: '请选择地区', trigger: 'change'}
                ],
                stationName: [
                    {required: true, message: '请输入站点名称', trigger: 'blur'},
                    {min: 1, max: 10, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ],
                stationSpell: [
                    {required: true, message: '请输入城市拼音简写', trigger: 'blur'},
                    {min: 1, max: 10, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ],
                sort: [
                    {required: true, message: '请输入排序', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            options: []
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        showStationUpdate(id) {
            this.dialogVisible = true
            this.getRegion()
            if (id) {
                this.getInfoById(id)
            } else {
                this.formData.id = 0
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
            }
        },
        getRegion(){
            RegionApi.getCacheRegion("region_district").then((response)=>{
                this.options = response
            })
        },

        // 获取详情
        getInfoById(id) {
            StationApi.getInfoById(id).then((data) => {
                this.formData = Common.assign(this.formData, data)
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        StationApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        StationApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>
